﻿using System;
using System.Data;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

/// <summary>
/// StreetNumId 的摘要说明
/// </summary>
namespace Housing
{
    public class StreetNumId
    {
        private int _id;
        private string _noid;
        private string _streetname;
        private string _letter;

        public StreetNumId()
        {

        }

        public int Str_id
        {
            get { return _id; }
            set { _id = value; }
        }

        public string Str_noid
        {
            get { return _noid; }
            set { _noid = value; }
        }

        public string Str_streetname
        {
            get { return _streetname; }
            set { _streetname = value; }
        }

        public string Str_letter
        {
            get { return _letter; }
            set { _letter = value; }
        }

        protected string connString = SqlHelper.ConnectionString;

        public bool AddNewStreetNum(StreetNumId streetnum)
        {
            string query = @"INSERT INTO StreetNumId(str_noid,str_streetname,str_letter) VALUES('" + streetnum.Str_noid + "','" + streetnum.Str_streetname + "','" + streetnum.Str_letter + "')";
            bool result = Convert.ToBoolean(SqlHelper.ExecuteNonQuery(connString, CommandType.Text, query));
            return result;
        }

        public bool UpdateAStreetNum(StreetNumId streetnum)
        {
            string query = "UPDATE StreetNumId SET str_noid='" + streetnum.Str_noid + "',str_streetname='" + streetnum.Str_streetname + "',str_letter='" + streetnum.Str_letter + "' WHERE str_id=" + streetnum.Str_id;
            bool result = Convert.ToBoolean(SqlHelper.ExecuteNonQuery(connString, CommandType.Text, query));
            return result;
        }

        public DataSet GetAllStreetNums()
        {
            string query = "SELECT * FROM StreetNumId";
            DataSet ds = SqlHelper.ExecuteDataset(connString, CommandType.Text, query);
            return ds;
        }

        public string GenerateLongId(string street, string longid)
        {
            string cmdnoid = @"SELECT TOP 1 str_noid FROM StreetNumId WHERE str_streetname=
                                '" + street + "街道" + "' ORDER BY str_id DESC";
            string strnoid = Convert.ToString(SqlHelper.ExecuteScalar(connString, CommandType.Text, cmdnoid)); 
            //判断是否找到街道编号
            if (!string.IsNullOrEmpty(strnoid))
            {
                if (!string.IsNullOrEmpty(longid))
                {
                    string cmdtext = @"SELECT COUNT(apply_id) FROM Apply WHERE cast(right(apply_longid,5) as int)=" + longid + " AND apply_street='" + street + "'";
                    int count = (Int32)SqlHelper.ExecuteScalar(connString, CommandType.Text, cmdtext);
                    if (count > 0)
                        return "";
                    else
                    {
                        string prelongid = "A" + strnoid;
                        if (longid.Length == 1)
                            return prelongid + "0000" + longid;
                        else if (longid.Length == 2)
                            return prelongid + "000" + longid;
                        else if (longid.Length == 3)
                            return prelongid + "00" + longid;
                        else if (longid.Length == 4)
                            return prelongid + "0" + longid;
                        else
                            return prelongid + longid;
                    }
                }
                else
                {

                    strnoid = "A" + strnoid;
                    string cmdlongid = "SELECT MAX(cast(right(apply_longid,8) as int)) FROM Apply WHERE left(apply_longid,4)='" + strnoid + "'";
                    object objid = SqlHelper.ExecuteScalar(SqlHelper.ConnectionString, CommandType.Text, cmdlongid);
                    //string rightid = "0";
                    if (!string.IsNullOrEmpty(objid.ToString()))
                    {
                        int rightid = (Int32)objid;
                        rightid++;
                        if (rightid.ToString().Length == 6)
                            return "A00" + rightid;
                        else if (rightid.ToString().Length == 7)
                            return "A0" + rightid;
                        else
                            return "A" + rightid;
                    }
                    else
                        return strnoid + "00001";
                }
            }
            else
                return "";
        }
    }
}